using System;
using System.ComponentModel;
using ErkerTech.DeltaScope.View.Models;
using BaseControl=ErkerTech.DeltaScope.View.Common.BaseControl;

namespace ErkerTech.DeltaScope.View.Controls
{
    public class SwirlDisplay : BaseControl 
    {
        #region Private Declarations
        public System.Windows.Forms.VScrollBar VScroller;
        private int _SwirlAreaWidth = 60;
        private bool _WordWrap;
        private bool _ShowSwirlRect = true;
        #endregion

        #region Event Declarations
        public delegate void RedrawHandler(object SwirlDisplay, EventArgs e);
        public event RedrawHandler Redraw;
        public delegate void SwirlCalculateHandler(SwirlDisplay SwirlDisplay, EventArgs e);
        public event SwirlCalculateHandler WillCalculate;
        public event SwirlCalculateHandler DidCalculate;
        #endregion

        #region Overridden PUBLIC EVENTS
        public delegate void ClickHandler (SwirlDisplay Control, SwirlDisplayClickEventArgs e);
        public new event ClickHandler Click;
        public new event ClickHandler DoubleClick;

        protected override void OnClick(EventArgs e)
        {
            // Don't call down to the base class...
            if (Click != null)
                Click(this, new SwirlDisplayClickEventArgs(new SwirlBlock(String.Empty,string.Empty), true));
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            if (DoubleClick != null)
                DoubleClick(this, new SwirlDisplayClickEventArgs(new SwirlBlock(String.Empty,String.Empty), true));

        }
        #endregion

        #region Properties
        [Category("Appearance")]
        [Description ("The width in pixels of the swirl area.")]
        [DefaultValue (typeof(int), "60")]
        public int SwirlAreaWidth
        {
            get { return _SwirlAreaWidth; }
            set { if (value >= 0) _SwirlAreaWidth = value; }
        }

        public int DrawWidth
        {
            get { return (VScroller.Visible) ? VScroller.Left : Width; }
        }

        [Category("Behavior")]
        [Description ("Indicates if lines are automatically word-wrapped.")]
        [DefaultValue (typeof(bool), "true")]
        public bool WordWrap
        {	
            get { return _WordWrap; }
            set	
            { 
                SwirlBlock.WordWrap = value;
                if (_WordWrap != value) Invalidate();
                _WordWrap = value; 
            }
        }

        [Category("Behavior")]
        [Description ("Indicates if the swirl region is framed by two vertical lines.")]
        [DefaultValue (typeof(bool), "true")]		
        public bool ShowSwirlRect
        {
            get { return _ShowSwirlRect; }
            set 
            { 
                bool lbNeedToInvalidate = (_ShowSwirlRect != value);
                _ShowSwirlRect = value; 
                if (lbNeedToInvalidate) Invalidate();
            }
        }
        #endregion

        #region Constructors
        public SwirlDisplay()
        {
            InitializeComponent();	
        }
        #endregion

        #region Component Designer generated code
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            VScroller = new System.Windows.Forms.VScrollBar();
            SuspendLayout();
            // 
            // VScroller
            // 
            VScroller.Dock = System.Windows.Forms.DockStyle.Right;
            VScroller.LargeChange = 2;
            VScroller.Location = new System.Drawing.Point(568, 0);
            VScroller.Maximum = 1;
            VScroller.Name = "VScroller";
            VScroller.Size = new System.Drawing.Size(16, 320);
            VScroller.SmallChange = 2;
            VScroller.TabIndex = 0;
            VScroller.Visible = false;
            // 
            // SwirlDisplay
            // 
            BackColor = System.Drawing.SystemColors.Window;
            Controls.Add(VScroller);
            Name = "SwirlDisplay";
            Size = new System.Drawing.Size(584, 320);
            ResumeLayout(false);

        }
        #endregion

        #region Public Methods
        public void Clear()
        {
            var loGraphics = CreateGraphics();
            loGraphics.Clear(BackColor);
            loGraphics.Dispose();
        }
        public void OnCalculate( bool tbRaiseWill)
        {
            if (tbRaiseWill && WillCalculate != null)
                WillCalculate(this, EventArgs.Empty);
            else if ((tbRaiseWill == false) && (DidCalculate != null))
                DidCalculate(this, EventArgs.Empty);
        }
        public void OnRedraw()
        {
            // Inform the controller...
            Redraw(this, EventArgs.Empty);
        }
        #endregion
    }

    public class SwirlDisplayClickEventArgs: EventArgs
    {
        public SwirlBlock Block { get; set; }

        public bool LeftSide { get; private set; }
        public bool RightSide { get; private set; }

        public SwirlDisplayClickEventArgs (SwirlBlock toBlock, bool tbLeftSideClicked)
        {
            Block = toBlock;
            LeftSide = tbLeftSideClicked;
            RightSide = !tbLeftSideClicked;
        }
    }
}